package RPPM.servlet;

import RPPM.dao.impl.OnsitestaffDaoImpl;
import RPPM.dao.impl.StuDaoImpl;
import RPPM.domain.Onsitestaff;
import RPPM.domain.ResultInfo;
import RPPM.domain.Student;
import RPPM.service.Impl.OnsitestaffServiceImpl;
import RPPM.service.OnsitestaffService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.Result;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@WebServlet("/onsiteservlet/*")
public class OnsiteServlet extends BaseServlet{
    /**
     * findTrainingMessage 查看培训信息
     * @param request
     * @param response
     * @throws IOException
     */
    public void findCourseMessage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");

        int Cou_id = (int) request.getSession().getAttribute("courseid");


        OnsitestaffDaoImpl onsitestaffDao = new OnsitestaffDaoImpl();

        List<String> list = onsitestaffDao.findCourseMessage(Cou_id);

        for(String string : list){
            System.out.println(string);
        }
        ResultInfo info = new ResultInfo();
        info.setData(list);

        writeJsonValue(response, info);
    }

    /**
     * ossChooseCourse 选择当前的课程
     * @param request
     * @param response
     * @throws IOException
     */
    public void ossChooseCourse(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        System.out.println("ossChooseCourse");

        System.out.println(request.getParameter("selectedCourse"));
        request.getSession().setAttribute("Cou_name",request.getParameter("selectedCourse"));
        ResultInfo info = new ResultInfo();
        info.setFlag(true);

        System.out.println(request.getSession().getAttribute("Cou_name"));

        writeJsonValue(response, info);
    }

    /**
     * ossMenu 获取所有课程
     * @param request
     * @param response
     * @throws IOException
     */
    public void ossMenu(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        System.out.println("ossMenu");


        int staff_id = (int) request.getSession().getAttribute("staff_id");
        System.out.println(staff_id);

        Onsitestaff tobechecked = new Onsitestaff();

        OnsitestaffService Onsitestaffservice= new OnsitestaffServiceImpl();

        System.out.println("session" + staff_id);


        ResultInfo info = new ResultInfo();

        response.setContentType("text/html;charset=utf-8");
        List<String> courses = new ArrayList<>();
        courses = Onsitestaffservice.getCourses();

        for(String str:courses)
            System.out.println(str);
        info.setData(courses);
        try {
            writeJsonValue(response, info);
            System.out.println("json回写成功");
        } catch (IOException e) {
            System.out.println("json回写失败");
        }

    }

    /**
     * ossCheckStudents 获取所选课程的全部学生
     * @param request
     * @param response
     * @throws IOException
     */
    public void ossCheckStudents(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        System.out.println("ossCheckStudents");

        String cou_name = (String) request.getSession().getAttribute("Cou_name");
        System.out.println(cou_name);

        OnsitestaffService onsitestaffService = new OnsitestaffServiceImpl();

        List<Map<String, Object>> students = new ArrayList<>();

        students = onsitestaffService.getStudentsByCouname(cou_name);
        System.out.println(students);

        ResultInfo info = new ResultInfo();
        info.setData(students);

        writeJsonValue(response, info);
    }

    /**
     * ossCheckStudentsEX 按照名字查找学生
     * @param request
     * @param response
     * @throws IOException
     */
    public void ossSearchStudents(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        System.out.println("ossSearchStudents");

        String cou_name = (String) request.getSession().getAttribute("Cou_name");
        String stu_name = request.getParameter("searchText");
        System.out.println(cou_name+stu_name);

        OnsitestaffService onsitestaffService = new OnsitestaffServiceImpl();

        List<Map<String, Object>> students;
        students = onsitestaffService.getStudentsByStuname(cou_name, stu_name);
        System.out.println(students);

        ResultInfo info = new ResultInfo();
        info.setData(students);

        writeJsonValue(response, info);
    }

    /**
     * ossPostSign 发布签到 仅针对当前选择的课程
     * @param request
     * @param response
     * @throws IOException
     */
    public void ossPostSign(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        System.out.println("ossPostSign");

        String cou_name = (String) request.getSession().getAttribute("Cou_name");
        System.out.println(cou_name);

        ResultInfo info = new ResultInfo();

        if (cou_name.isEmpty()){
            info.setFlag(false);
        } else {
            OnsitestaffService onsitestaffService = new OnsitestaffServiceImpl();
            onsitestaffService.postSign(cou_name);
            info.setFlag(true);
        }

        writeJsonValue(response, info);

    }

    /**
     * ossPostAdvice 发布反馈 仅针对当前选择的课程
     * @param request
     * @param response
     * @throws IOException
     */
    public void ossPostAdvice(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        System.out.println("ossPostAdvice");

        String cou_name = (String) request.getSession().getAttribute("Cou_name");
        System.out.println(cou_name);

        ResultInfo info = new ResultInfo();

        if (cou_name.isEmpty()){
            info.setFlag(false);
        } else {
            OnsitestaffService onsitestaffService = new OnsitestaffServiceImpl();
            onsitestaffService.postAdvice(cou_name);
            info.setFlag(true);
        }

        writeJsonValue(response, info);

    }

    /**
     * ossStatisticsSign 统计签到情况 仅针对当前选择的课程
     * @param request
     * @param response
     * @throws IOException
     */
    public void ossStatisticsSign(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        System.out.println("ossStatisticsSign");

        String cou_name = (String) request.getSession().getAttribute("Cou_name");
        System.out.println(cou_name);

        ResultInfo info = new ResultInfo();

        if (cou_name.isEmpty()){
            System.out.println("false");
            info.setFlag(false);
        } else {
            System.out.println("true");
            List<Map<String, Object>> signs;
            OnsitestaffService onsitestaffService = new OnsitestaffServiceImpl();
            signs = onsitestaffService.getStatisticsSign(cou_name);

            for(Map<String, Object> map: signs){
                for(String key:map.keySet()){
                    if(key.equals("Stu_sign")){
                        System.out.println("change");
                        if (map.get(key).equals("0")){
                            map.replace(key, "未签到");
                        } else if (map.get(key).equals("1")){
                            map.replace(key, "已签到");
                        }
                    }
                    if(key.equals("Cou_time")){
                        System.out.println(map.get(key).getClass());
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        Object o = map.get(key);
                        map.replace(key, df.format(o));
                        System.out.println(map.get(key).getClass());
                        System.out.println(map.get(key));
                    }
                }
            }
            System.out.println(signs);
            info.setFlag(true);
            info.setData(signs);
        }

        writeJsonValue(response, info);
    }

    /**
     * ossStatisticsAdvice 统计反馈情况 仅针对当前选择的课程
     * @param request
     * @param response
     * @throws IOException
     */
    public void ossStatisticsAdvice(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        System.out.println("ossStatisticsAdvice");

        String cou_name = (String) request.getSession().getAttribute("Cou_name");
        System.out.println(cou_name);

        ResultInfo info = new ResultInfo();

        if (cou_name.isEmpty()){
            info.setFlag(false);
        } else {
            List<Map<String, Object>> advices;
            OnsitestaffService onsitestaffService = new OnsitestaffServiceImpl();

            advices = onsitestaffService.getStatisticsAdvice(cou_name);
            for(Map<String, Object> map: advices){
                for(String key:map.keySet()){
                    if(key.equals("Cou_advice")){
                        System.out.println("change");
                        if (map.get(key).equals("0")){
                            map.replace(key, "未反馈");
                        }
//                        else {
//                            map.replace(key, "已反馈");
//                        }
                    }
                    System.out.println(map.get(key));
                }
            }
            System.out.println(advices);
            info.setFlag(true);
            info.setData(advices);
        }

        writeJsonValue(response, info);
    }

}
